package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Product;
import cn.wolfcode.trip.base.domain.ProductInfo;
import cn.wolfcode.trip.base.mapper.ProductInfoMapper;
import cn.wolfcode.trip.base.mapper.ProductMapper;
import cn.wolfcode.trip.base.query.ProductQueryObject;
import cn.wolfcode.trip.base.service.IProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductInfoMapper productInfoMapper;
    @Override
    public PageInfo<Product> queryProduct(ProductQueryObject qo) {
        qo.setOrderBy("p.addTime desc");
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Product> list = productMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void saveOrUpdate(Product entity) {
        ProductInfo productInfo = entity.getProductInfo();
        if (entity.getId()!=null){
            entity.setAddTime(new Date());
            productMapper.updateByPrimaryKey(entity);
            productInfo.setId(entity.getId());
            productInfoMapper.updateByPrimaryKey(productInfo);
        } else {

            entity.setAddTime(new Date());
            productMapper.insert(entity);
            productInfo.setId(entity.getId());
            productInfoMapper.insert(productInfo);
        }
    }

    @Override
    public Product getProductById(Long id) {
        return productMapper.selectByPrimaryKey(id);

    }

    @Override
    public String getInfo(Long id) {
        return productInfoMapper.selectByPrimaryKey(id).getProductInfo();
    }

    @Override
    public void changeState(Long id, Integer state) {
        productMapper.changeState(id,state,null);
    }

    @Override
    public List<Product> getProductByType(Long productTypeId) {
        return productMapper.selectByType(productTypeId);
    }

    @Override
    public List<Product> getNewProduct() {
        return productMapper.selectNewProduct();
    }
}
